<?php
/**
 * User: wanghousheng
 * Date: 2017/9/20
 * Time: 10:26
 * desc: 快递鸟API
 */
class Express_Lite{
    private $appid  = '1267194';
    private $appkey = '3ae50e9b-8901-4f85-a3f7-1f1de401e555';

    public function __construct($appid='',$appkey='')
    {
        if ($appid) $this->appid = $appid;
        if ($appkey) $this->appkey = $appkey;
    }

    /**
     * @brief 获取api提交的url地址
     * @return string url地址
     */
    public function getSubmitUrl()
    {
        return 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
    }

    /**
     * @brief 获取物流轨迹线路
     * @param $ShipperCode string 物流公司代号
     * @param $LogisticCode string 物流单号
     * @return string array 轨迹数据
     */
    public function line($ShipperCode,$LogisticCode)
    {
        $params = array(
            'ShipperCode' => $ShipperCode,
            'LogisticCode'=> $LogisticCode,
        );

        $sendData = json_encode($params);
        $curlData = array(
            'RequestData' => urlencode($sendData),
            'EBusinessID' => $this->appid,
            'RequestType' => '1002',
            'DataType'    => 2,
            'DataSign'    => urlencode(base64_encode(md5($sendData.$this->appkey))),
        );
        $result     = $this->curlSend($this->getSubmitUrl(),$curlData);
        $resultJson = json_decode($result,true);

        if(!$resultJson)
        {
            die(var_export($result));
        }
        return $this->response($resultJson);
    }

    /**
     * @brief 处理返回数据统一数据格式
     * @param $result 结果处理
     * @return array 通用的结果集 array('result' => 'true或者false','data' => array( array('time' => '时间','context' => '地点'),......),'reason' => '失败原因')
     */
    public function response($result)
    {
        $status = false;
        $data   = array();
        $message= "查询成功";
        $state = 0;
        if(isset($result['Traces']) && $result['Traces'])
        {
            foreach($result['Traces'] as $key => $val)
            {
                $data[$key]['time']   = $val['AcceptTime'];
                $data[$key]['context']= $val['AcceptStation'];
            }
            $state = $result['State'];
            $status = true;
        }

        if(isset($result['Message']))
        {
            $message = $result['Message'];
        }
        else if(isset($result['Reason']))
        {
            $message = $result['Reason'];
        }
        return array('result' => $status,'data' => $data,'msg' => $message,'state'=>$state);
    }

    /**
     * @brief CURL模拟提交数据
     * @param $url string 提交的url
     * @param $data array 要发送的数据
     * @return mixed 返回的数据
     */
    private function curlSend($url,$data)
    {
        $data = $this->encodeData($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        return curl_exec($ch);
    }

    //进行数据的string字符串编码
    private function encodeData($datas)
    {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }
        $post_data = join('&', $temps);
        return $post_data;
    }
}